package quanlydoanra.theotieuchithoigian;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.faces.model.SelectItem;

import utils.DateUtils;

import dbutil.ConnectDB;

public class TieuChiThoiGian {
	private String error;
	private java.util.Date ngayBatDau;
	private java.util.Date ngayKetThuc;
	private String thongTin;
	private String tieuChi;
	private List<ThongTin> listThongTin = null;
	private String tongDoanRa;
	private SelectItem[] dsTieuChi ;
	
	public TieuChiThoiGian(){}

	public TieuChiThoiGian(Date ngayBatDau, Date ngayKetThuc,
			String thongTin, String tieuChi, List<ThongTin> listThongTin,
			String tongDoanRa, List<String> dsTieuChi) {
		this.ngayBatDau = ngayBatDau;
		this.ngayKetThuc = ngayKetThuc;
		this.thongTin = thongTin;
		this.tieuChi = tieuChi;
		this.listThongTin = listThongTin;
		this.tongDoanRa = tongDoanRa;		
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public java.util.Date getNgayBatDau() {
		return ngayBatDau;
	}

	public void setNgayBatDau(java.util.Date ngayBatDau) {
		this.ngayBatDau = ngayBatDau;
	}

	public java.util.Date getNgayKetThuc() {
		return ngayKetThuc;
	}

	public void setNgayKetThuc(java.util.Date ngayKetThuc) {
		this.ngayKetThuc = ngayKetThuc;
	}

	public String getThongTin() {
		return thongTin;
	}

	public void setThongTin(String thongTin) {
		this.thongTin = thongTin;
	}

	public String getTieuChi() {
		return tieuChi;
	}

	public void setTieuChi(String tieuChi) {
		this.tieuChi = tieuChi;
	}

	public List<ThongTin> getListThongTin() {
		if (this.listThongTin == null){
			this.listThongTin =  new ArrayList<ThongTin>();;
			}
			return listThongTin;
	}

	public void setListThongTin(List<ThongTin> listThongTin) {
		this.listThongTin = listThongTin;
	}

	public String getTongDoanRa() {
		return tongDoanRa;
	}

	public void setTongDoanRa(String tongDoanRa) {
		this.tongDoanRa = tongDoanRa;
	}

	
	/**************************************
	 * Method name		: listItemTieuChi
	 * Return type		: SelectItem[]
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public SelectItem[] listItemTieuChi() {		
		SelectItem[] arrItem = new SelectItem[5];
		arrItem[0] = new SelectItem("tennguoidi","Tên người đi");
		arrItem[1] = new SelectItem("coquan","Cơ quan");
		arrItem[2] = new SelectItem("mucdich","Mục đích");
		arrItem[3] = new SelectItem("ngaydi","Thời gian đi");
		arrItem[4] = new SelectItem("donvimoi","Đơn vị mời");
		
		return arrItem;
	}
		
	public SelectItem[] getDsTieuChi() {
		this.dsTieuChi=  this.listItemTieuChi();
		return dsTieuChi;		
	}

	public void setDsTieuChi(SelectItem[] dsTieuChi) {
		this.dsTieuChi = dsTieuChi;
	}

	/**************************************
	 * Method name		: thongke
	 * Return type		: String
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public String thongke(){
		this.setError("");
		String sql1 = "select DS.tennguoidi,CN.coquan,DR.ngaydi,DR.donvimoi,MD.mucdich,DR.chiphi from dsnguoidi as DS ,doanra as DR,MucDich as MD,canhan as CN where ";
		String sql2 =" and DS.iddoanra = DR.madoan and DR.idmucdich = MD.id and CN.cmnd = DS.cmnd and '"+DateUtils.utilDateToSqlDate(this.ngayBatDau) +"' < ngaydi and ngaydi < '"+DateUtils.utilDateToSqlDate(this.ngayKetThuc)+"'";
		String sql = "";
		System.out.println("Tieu chi "+this.tieuChi);
		int acc =0;
		if(this.tieuChi.equals("tennguoidi")){
			sql = sql1 + " DS.tennguoidi like '%"+this.thongTin+"%'" +sql2;
			acc =1;
		}
		else if(this.tieuChi.equals("mucdich")){
			acc =1;
			sql =  sql1 +"MD.mucdich like '%"+this.thongTin+"%'"+sql2;
		}
		else if(this.tieuChi.equals("ngaydi")){
			if(DateUtils.checkIsDate(this.thongTin)){
				acc =1;
			sql = sql1 + " DR.ngaydi = '"+java.sql.Date.valueOf(this.thongTin)+"'"+sql2;
			}
			else {
				acc =0;
				this.setError("Thông tin nhập vào có kiểu là ngày tháng");
			}
			
		}
		else if(this.tieuChi.equals("coquan")){
			acc =1;
			sql =sql1 + "CN.coquan like '%"+this.thongTin+"%'"+sql2;
		}
		else if(this.tieuChi.equals("donvimoi")){
			acc =1;
			sql =sql1 + " DR.donvimoi like '%"+this.thongTin+"%'" +sql2;
		}
		System.out.print("SQL "+sql);
		if(acc ==1 && (this.thongTin != null||!this.thongTin.equals(""))){
		List<ThongTin> listThongTin = new ArrayList<ThongTin>();
			try{
				ArrayList<HashMap<String, Object>> array = ConnectDB.getDataTypeList(sql);
				if (array.size() > 0) {
					HashMap<String, Object> hash = new HashMap<String, Object>();
					int stt = 0;
					for (int i = 0; i < array.size(); i++) {
						stt = i + 1;
						hash = (HashMap<String, Object>) array.get(i);
						ThongTin infor = new ThongTin();
						infor.setSTT(String.valueOf(stt));
						infor.setTenNguoiDi(hash.get("tennguoidi").toString());
						infor.setCoQuan(hash.get("coquan").toString());
						infor.setMucDich(hash.get("mucdich").toString());
						infor.setNgayDi(hash.get("ngaydi").toString());
						infor.setChiPhi(hash.get("chiphi").toString());
						infor.setDonViMoi(hash.get("donvimoi").toString());
						listThongTin.add(infor);
					}
				}else {
					this.setError("Không tìm thấy thông tin!");
				}
				this.setTongDoanRa("Tổng bảng biểu : "+array.size());
			}catch(Exception ex){
				
			}		
		this.setListThongTin(listThongTin);
		}
	
		return "tieuchithoigian";
	}
	/**************************************
	 * Method name		: huy
	 * Return type		: String
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public String huy(){
		this.ngayBatDau= null;
		this.ngayKetThuc = null;
		this.thongTin ="";
		this.error ="";
		this.listThongTin =null;
		return "tieuchithoigian";
	}
	
	
	

}
